<?php
namespace Admin\Controller;
use Think\Controller;
class PublicController extends Controller {
	
	//单文件上传
	public function uploadOne($files){    
		$upload = new \Think\Upload();// 实例化上传类  
	  	$upload->maxSize   =     3145728 ;// 设置附件上传大小 
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
		$upload->savePath  =      'Uploads/'; // 设置附件上传目录    // 上传文件 
		$info   =   $upload->uploadOne($_FILES[$files]);
		if(!$info) {// 上传错误提示错误信息    
			$this->error($upload->getError()); 
			die();   
		}else{// 上传成功     
			//$this->success('上传成功！');
			return $info;    
		}
		
	}
	
	
	//多文件上传
	public function upload(){    
		$upload = new \Think\Upload();// 实例化上传类  
	  	$upload->maxSize   =     3145728 ;// 设置附件上传大小 
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 
		$upload->savePath  =      'Uploads/'; // 设置附件上传目录    // 上传文件 
		$info   =   $upload->upload();
		if(!$info) {// 上传错误提示错误信息    
			$this->error($upload->getError());    
		}else{// 上传成功     
			$this->success('上传成功！');    
		}
	}
	
	
	//生成缩略图
	public function thumb($filesinfo,$type=1,$width=100,$height=100){	
		$image = new \Think\Image();
		$image->open($filesinfo);// 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
		$thumb1=dirname($filesinfo).'/thumb_'.basename($filesinfo);
		$thumb=str_ireplace("water_","",$thumb1);
		switch($type){
			case 1:
				$image->thumb($width, $height,\Think\Image::IMAGE_THUMB_SCALE)->save($thumb);//等比例缩放类型
			break;
			case 2:
				$image->thumb($width, $height,\Think\Image::IMAGE_THUMB_FILLED)->save($thumb);//缩放后填充类型
			break;
			case 3:
				$image->thumb($width, $height,\Think\Image::IMAGE_THUMB_CENTER)->save($thumb);//居中裁剪类型
			break;
			case 4:
				$image->thumb($width, $height,\Think\Image::IMAGE_THUMB_NORTHWEST)->save($thumb);//左上角裁剪类型
			break;
			case 5:
				$image->thumb($width, $height,\Think\Image::IMAGE_THUMB_SOUTHEAST)->save($thumb);//右下角裁剪类型
			break;
			case 6:
				$image->thumb($width, $height,\Think\Image::IMAGE_THUMB_FIXED)->save($thumb);//固定尺寸缩放类型
			break;		
			default:
				$image->thumb($width, $height,\Think\Image::IMAGE_THUMB_SCALE)->save($thumb);//默认缩放方式

			}
	}
	
	
	//生成图片水印
	public function water($dst_file,$water_file = 'watermark.png',$postion=5){
		
		$image = new \Think\Image(); // 在图片左上角添加水印（水印文件位于./logo.png） 并保存为water.jpg
		$water=dirname($dst_file).'/water_'.basename($dst_file);
		switch($postion){
			case 1:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_NORTHWEST)->save($water); //左上角水印
			break;
			case 2:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_NORTH)->save($water); //上居中水印
			break;
			case 3:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_NORTHEAST)->save($water); //右上角水印
			break;
			case 4:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_WEST)->save($water); //左居中水印
			break;
			case 5:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_CENTER)->save($water); //居中水印
			break;
			case 6:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_EAST)->save($water); //右居中水印
			break;
			case 7:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_SOUTHWEST)->save($water); //左下角水印
			break;		
			case 8:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_SOUTH)->save($water); //下居中水印
			break;		
			case 9:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_SOUTHEAST)->save($water); //右下角水印
			break;				
			default:
				$image->open($dst_file)->water($water_file,\Think\Image::IMAGE_WATER_NORTHWEST)->save($water); //默认方式

			}
			
			return $water;    

		}
	
}